package com.zhyt.intelligentpipeline.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhyt.intelligentpipeline.mapper.CorrosionDetectionMapper;
import com.zhyt.intelligentpipeline.mapper.DeformationDetectionMapper;
import com.zhyt.intelligentpipeline.mapper.FailureSafetyPressureMapper;
import com.zhyt.intelligentpipeline.mapper.PipelineMapper;
import com.zhyt.intelligentpipeline.model.*;
import com.zhyt.intelligentpipeline.service.WorkingConditionService;
import com.zhyt.intelligentpipeline.vo.ResidualStrengthResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class WorkingConditionServiceImpl implements WorkingConditionService {

    @Resource
    private CorrosionDetectionMapper corrosionDetectionMapper;

    @Resource
    private DeformationDetectionMapper deformationDetectionMapper;

    @Resource
    private PipelineMapper pipelineMapper;

    @Resource
    private FailureSafetyPressureMapper failureSafetyPressureMapper;

    @Override
    public List<CorrosionDetection> getPipelineCorrosionInfo(String initialStation, String endStation) {
        QueryWrapper<CorrosionDetection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CorrosionDetection::getInitialStation,initialStation).eq(CorrosionDetection::getEndStation,endStation);
        return corrosionDetectionMapper.selectList(queryWrapper);
    }

    @Override
    public List<DeformationDetection> getPipelineDeformation(String initialStation, String endStation) {
        QueryWrapper<DeformationDetection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DeformationDetection::getInitialStation,initialStation).eq(DeformationDetection::getEndStation,endStation);
        return deformationDetectionMapper.selectList(queryWrapper);
    }

    @Override
    public FailureSafetyPressure getFailureSafetyPressure(String corrosionId,Double f1,Double f2) {

            FailureSafetyPressure failureSafetyPressure = new FailureSafetyPressure();
            //todo 通过corrosionId获取基本信息
            ResidualStrength residualStrength = new ResidualStrength();
            CorrosionDetection corrosionDetection = corrosionDetectionMapper.selectById(corrosionId);
            //设置腐蚀缺陷的长度
            residualStrength.setCorrosionDefectLength(Double.parseDouble(corrosionDetection.getLength()));
            //设置腐蚀缺陷深度
            residualStrength.setCorrosionDefectDepth(Double.parseDouble(corrosionDetection.getDepth()));
            //设置壁厚
            residualStrength.setPipeNominalWallThickness(Double.parseDouble(corrosionDetection.getWallThickness()));
            //todo 通过起点站和终点站获取管段的基本信息
            String initStation = corrosionDetection.getInitialStation();
            String endStation = corrosionDetection.getEndStation();
            LambdaQueryWrapper<Pipeline> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Pipeline::getEndStation,endStation).eq(Pipeline::getInitialStation,initStation);
            Pipeline pipeline = pipelineMapper.selectOne(queryWrapper);
            Double pressureByGrade = GradePressure.getPressureByGrade(pipeline.getGrade());
            //设置最小拉伸强度
            residualStrength.setTensileStrength(pressureByGrade);
            //设置管道直径
            residualStrength.setPipeOuterDiameter(Double.parseDouble(pipeline.getDiameter().replaceAll("[^0-9]", "")));
            //设置设计压力
            residualStrength.setDesignPressure(Double.parseDouble(pipeline.getDesignPress().replaceAll("[^0-9]", "")));
            //设置模型系数
            residualStrength.setModelCoefficient(f1);
            //设置操作使用系数
            residualStrength.setUsingCoefficients(f2);
            ResidualStrengthResult instance = ResidualStrengthResult.getInstance(residualStrength);
            failureSafetyPressure.setFailurePressure(instance.getFailurePressure());
            failureSafetyPressure.setSafetyPressure(instance.getSafeWorkingPressure());
            failureSafetyPressure.setCommend(instance.getComment());
            failureSafetyPressure.setCorrosionId(corrosionId);
            //todo 将数据保存到数据库中
            //查询数据库中是否有这个腐蚀信息的预测信息
            FailureSafetyPressure existValue = failureSafetyPressureMapper.selectById(corrosionId);
            if(existValue != null){
                failureSafetyPressureMapper.updateById(failureSafetyPressure);
            }else{
                failureSafetyPressureMapper.insert(failureSafetyPressure);
            }
        return failureSafetyPressure;
    }

    @Override
    public FailureSafetyPressure getFailureSafetyPressureByCorId(String corrosionId) {
        return failureSafetyPressureMapper.selectById(corrosionId);
    }

    public static void main(String[] args) {
        String a = "φ813";
        String s = a.replaceAll("[^0-9]", "");
        System.out.println(s);
    }
}
